package com.cn.por.biz.user.controller;

import java.util.List;

import javax.annotation.security.RolesAllowed;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.cn.por.biz.user.dto.ProUserDetails;
import com.cn.por.biz.user.dto.UserDO;
import com.cn.por.biz.user.role.dto.UserRole;
import com.cn.por.biz.user.service.UserService;
import com.cn.por.util.jwt.JWTUtil;
import cn.hsa.base.web.vo.WrapperResponse;

@RestController
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@RolesAllowed("ROLE_admin")
	@GetMapping("/getUser")
	public List<ProUserDetails> getUser() {
		return userService.getUserList();
	}
	
	@PostMapping("/addUser")
	public int addUser(@RequestBody ProUserDetails user) {
		return userService.addUser(user);
	}
	
	@PostMapping("/addUserRole")
	public int addUserRole(@RequestBody UserRole userRole) {
		return userService.addUserRole(userRole);
	}
	
	/**
	 * 根据token字符串获取用户信息
	 * @param token
	 */
	@GetMapping("/getUserInfo")
	public WrapperResponse<UserDO> getUserInfo(String token) {
		List<String> roles =JWTUtil.getRole(token);
		UserDO user = new UserDO();
		user.setRoles(roles);
		user.setUsername(JWTUtil.getUsername(token));
		user.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
		user.setIntroduction("super"+user.getUsername());
		return WrapperResponse.success(user);
	}
}
